import {createRouter, createWebHistory, RouteRecordRaw,createWebHashHistory} from 'vue-router'

import Login from '@/views/Login/index.vue'
import Layout from '@/layout/index.vue'

import {getToken} from "@/utils/auth";
import {message} from "ant-design-vue";




const routes: Array<RouteRecordRaw> = [
    {
        path: '/login',
        component: Login,
        meta:{icon:'',title:'欢迎回来'}
    }, {
        path: '/',
        component: Layout,
        redirect: '/blogdata',
        children: [{
            name: "BlogData",
            path: 'blogdata',
            component: () => import('@/views/BlogData/index.vue'),
            meta:{
                icon:'',
                title:'博客数据',
                breadCrumber:{
                    path:['/'],
                    title:['博客数据']
                }
            }
        }]
    }, {
        path: '/articles',
        component: Layout,
        redirect:'/articles/index',
        children: [{
            path: 'index',
            component: () => import('@/views/Articles/index.vue'),
            alias:'/articles',
            meta:{
                icon:'',
                title:'文章列表',
                breadCrumber:{
                    path:['/articles','/articles'],
                    title:['文章管理','文章列表']
                }
            }
        }]
    },
    {
        path:'/creation',
        component:Layout,
        children:[{
            path:'index',
            component:() => import('@/views/Creation/index.vue'),
            alias: '/creation',
            meta:{
                icon:'',
                title:'创作',
                breadCrumber:{
                    path:['/creation'],
                    title:['创作']
                }
            }
        }]
    },
    {
        path:'/users',
        component:Layout,
        children:[{
            path:'index',
            component:() => import('@/views/User/index.vue'),
            alias: '/users',
            meta:{
                icon:'',
                title:'用户列表',
                breadCrumber:{
                    path:['/users','/users'],
                    title:['用户管理','用户列表']
                }
            }
        }]
    },{
        path:'/cates',
        component:Layout,
        redirect:'/cates/list',
        children:[{
            path:'list',
            component:() => import('@/views/Cate/CateList/index.vue'),
            alias: '/cates',
            meta:{
                icon:'',
                title:'分类列表',
                breadCrumber:{
                    path:['/cates','/cates'],
                    title:['分类管理','分类列表']
                }
            }
        },{
            path:'add',
            component:() => import('@/views/Cate/AddCate/index.vue'),
            alias: '/addCate',
            meta:{
                icon:'',
                title:'分类编辑',
                breadCrumber:{
                    path:['/cates','/addCate'],
                    title:['分类管理','新增分类']
                }
            }
        }]
    },{
        path:'/tags',
        component:Layout,
        redirect:'/tags/list',
        children:[{
            path:'list',
            component:() => import('@/views/Tag/TagList/index.vue'),
            alias: '/tags',
            meta:{
                icon:'',
                title:'标签列表',
                breadCrumber:{
                    path:['/tags','/tags'],
                    title:['标签管理','标签列表']
                }
            }
        },{
            path:'addTag',
            component:() => import('@/views/Tag/AddTag/index.vue'),
            alias: '/addTag',
            meta:{
                icon:'',
                title:'新增标签',
                breadCrumber:{
                    path:['/tags','/addTag'],
                    title:['标签管理','新增标签']
                }
            }
        }]
    },{
        path: '/friendlink',
        component: Layout,
        children: [{
            name: "FriendLink",
            path: 'index',
            alias: '/friendlink',
            component: () => import('@/views/FriendLink/index.vue'),
            meta:{
                icon:'',
                title:'友链管理',
                breadCrumber:{
                    path:['/friendlink'],
                    title:['友链管理']
                }
            }
        }]
    }
]

const router = createRouter({
    history: process.env.NODE_ENV == 'development' ? createWebHashHistory(process.env.BASE_URL) : createWebHistory(),
    routes
})


const whiteList = ['/','/login']

router.beforeEach((to,from,next) => {
    // 在白名Dan
    if(whiteList.includes(to.path)){
        return  next()
    }else {
        // 不再白名单 判断是否有token
        if(getToken() == '') {
            message.info('请先登录')
            return next('/login')
        }
        return next()
    }
})


export default router
